package com.ruoyi.report_sdj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_sdj.domain.SdjMglbljlBaDetail;
import com.ruoyi.report_sdj.domain.vo.SdjMglbljlBaDetailVo;
import com.ruoyi.report_sdj.mapper.SdjMglbljlBaDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sdj.domain.bo.SdjMglbljlBsInfoBo;
import com.ruoyi.report_sdj.domain.vo.SdjMglbljlBsInfoVo;
import com.ruoyi.report_sdj.domain.SdjMglbljlBsInfo;
import com.ruoyi.report_sdj.mapper.SdjMglbljlBsInfoMapper;
import com.ruoyi.report_sdj.service.ISdjMglbljlBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 锚杆拉拔力试验检测记录
Service业务层处理
 *
 * @author zyb
 * @date 2024-07-23
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class SdjMglbljlBsInfoServiceImpl implements ISdjMglbljlBsInfoService {

    private final SdjMglbljlBsInfoMapper baseMapper;
    private final SdjMglbljlBaDetailMapper detailMapper;

    /**
     * 查询锚杆拉拔力试验检测记录
     */
    @Override
    @FIllBasicInfo
    public SdjMglbljlBsInfoVo queryById(Long excelId){
        SdjMglbljlBsInfoVo sdjMglbljlBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<SdjMglbljlBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<SdjMglbljlBaDetail>()
            .eq(SdjMglbljlBaDetail::getExcelId, excelId)
            .orderByAsc(SdjMglbljlBaDetail::getXuh));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            sdjMglbljlBsInfoVo.setBaDetailVos(baDetailVos);
        }
        return sdjMglbljlBsInfoVo;
    }

    /**
     * 查询锚杆拉拔力试验检测记录列表
     */
    @Override
    public TableDataInfo<SdjMglbljlBsInfoVo> queryPageList(SdjMglbljlBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SdjMglbljlBsInfo> lqw = buildQueryWrapper(bo);
        Page<SdjMglbljlBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询锚杆拉拔力试验检测记录列表
     */
    @Override
    public List<SdjMglbljlBsInfoVo> queryList(SdjMglbljlBsInfoBo bo) {
        LambdaQueryWrapper<SdjMglbljlBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SdjMglbljlBsInfo> buildQueryWrapper(SdjMglbljlBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SdjMglbljlBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, SdjMglbljlBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, SdjMglbljlBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), SdjMglbljlBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), SdjMglbljlBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), SdjMglbljlBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), SdjMglbljlBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), SdjMglbljlBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), SdjMglbljlBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), SdjMglbljlBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), SdjMglbljlBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), SdjMglbljlBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), SdjMglbljlBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), SdjMglbljlBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), SdjMglbljlBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), SdjMglbljlBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), SdjMglbljlBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), SdjMglbljlBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), SdjMglbljlBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), SdjMglbljlBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), SdjMglbljlBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), SdjMglbljlBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(bo.getShejlbl() != null, SdjMglbljlBsInfo::getShejlbl, bo.getShejlbl());
        return lqw;
    }

    /**
     * 新增锚杆拉拔力试验检测记录
     */
    @Override
    public Boolean insertByBo(SdjMglbljlBsInfoBo bo) {
        SdjMglbljlBsInfo add = BeanUtil.toBean(bo, SdjMglbljlBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 18
            ArrayList<SdjMglbljlBaDetail> details = new ArrayList<>(18);
            for (long i = 1; i <= 18; i++) {
                details.add(new SdjMglbljlBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改锚杆拉拔力试验检测记录
     */
    @Override
    public Boolean updateByBo(SdjMglbljlBsInfoBo bo) {
        SdjMglbljlBsInfo update = BeanUtil.toBean(bo, SdjMglbljlBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), SdjMglbljlBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SdjMglbljlBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除锚杆拉拔力试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
